<template>
  <div class="page-department">
    <div class="component-mainHeader">
      <div class="top">
        <div class="topleft">
          <p>
            <span class="iconfont" style="color:#1abc9a;margin-right:3px;">&#xe607;</span>施工队账号管理
          </p>
        </div>
        <div class="topright">
          <el-button type="info" @click="$router.go(0)">
            <span style="font-weight:700;margin-right:5px;" class="iconfont">&#xe662;</span>刷新
          </el-button>
        </div>
      </div>
    </div>

    <div class="component-listHeader">
      <div class="search-header">
        <div class="left-title">
          <p class="left-text">
            <span class="iconfont">&#xe61e;</span>
            {{title}}
          </p>
          <!-- 添加按钮 -->
          <span
            class="upload"
            style="
              display:inline-block;
              color:#666;width: 105px;
              height: 30px;
              border: 1px solid #e4e4e4;
              background-color:#fff;
              font-size:14px;
              line-height:30px;
              margin-right:30px;
              margin-top:5px;
              "
            @click="addCount"
          >+&nbsp;&nbsp;添加账户</span>
        </div>
      </div>
    </div>
    <div class="table">
      <el-table ref="multipleTable" :data="info" tooltip-effect="dark" style="width:100%" border>
        <el-table-column label="施工队" align="center" prop="orgName"></el-table-column>
        <el-table-column label="登录账号" align="center" prop="userName"></el-table-column>
        <!--<el-table-column label="登录密码" align="center" prop="userPwd"></el-table-column>-->
        <el-table-column label="账号状态" align="center" prop="userStatus">
          <template slot-scope="scope">
            <div v-if="scope.row.userStatus == 0">停用</div>
            <div v-else>正常</div>
          </template>
        </el-table-column>
        <el-table-column label="操作信息" align="center">
          <template slot-scope="scope">
            <a @click="manage(scope.row)">管理</a>
          </template>
        </el-table-column>
      </el-table>
      <!--<span class="btn-search">提交</span>-->
    </div>

    <!-- 项目专业弹窗 -->
    <!-- 添加弹窗 -->
    <div class="component-model">
      <el-dialog :title="modelTitle" :visible.sync="dialogVisible" width="600px">
        <div>
          <el-form
            :model="ruleForm"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm addMater"
            style="font-size: 12px"
          >
            <el-form-item label="施工队：" style="width:316px;margin-left: 122px;" prop="team">
              <div class="icon">
                <el-select v-model="ruleForm.team" placeholder="请选择施工队">
                  <el-option
                    v-for="(item,index) in teamList"
                    :key="index"
                    :label="item.orgName"
                    :value="item.orgId"
                  ></el-option>
                </el-select>
              </div>
            </el-form-item>
            <el-form-item
              label="手机号："
              prop="userPhone"
              style="width: 317px;margin:30px auto;font-size:12px;"
            >
              <el-input v-model="ruleForm.userPhone" placeholder="请输入手机号"></el-input>
            </el-form-item>
            <el-form-item
              label="身份证号："
              prop="idcardNo"
              style="width: 317px;margin:30px auto;font-size:12px;"
            >
              <el-input v-model="ruleForm.idcardNo" placeholder="请输入身份证号"></el-input>
            </el-form-item>
            <el-form-item
              label="登录账号："
              prop="orgName"
              style="width: 317px;margin:30px auto;font-size:12px;"
            >
              <el-input v-model="ruleForm.orgName" placeholder="请输入账号"></el-input>
            </el-form-item>

            <el-form-item
              label="登录密码："
              prop="nickname"
              style="width:317px;margin:30px auto;font-size:12px !impotant;"
            >
              <el-input v-model="ruleForm.nickname" placeholder="请输入密码"></el-input>
            </el-form-item>
            <el-form-item>
              <a @click="resetForm('ruleForm')">取消</a>
              <a @click="submitForm('ruleForm')">确定</a>
            </el-form-item>
          </el-form>
        </div>
      </el-dialog>
    </div>

    <!-- 管理弹窗 -->

    <div class="component-model">
      <el-dialog :title="Title" :visible.sync="dialogV" width="600px">
        <div>
          <el-form
            :model="ruleForms"
            :rules="rules"
            ref="ruleForm"
            label-width="100px"
            class="demo-ruleForm"
            style="font-size: 12px"
          >
            <el-form-item label="施工队：" style="width:325px;margin-left: 116px;">
              <div class="icon">
                <el-select disabled="disabled" v-model="infoFan.orgName" placeholder="请选择施工队">
                  <el-option
                    :label="infoFan.orgName"
                    :value="infoFan.orgName"
                  ></el-option>
                </el-select>
              </div>
            </el-form-item>

            <el-form-item label="账号设置：" style="width:325px;margin-left: 116px;">
              <div class="icon">
                <el-select v-model="infoFan.userStatus" placeholder="请选择状态">
                  <el-option label="正常" value="1"></el-option>
                  <el-option label="停用" value="0"></el-option>
                </el-select>
              </div>
            </el-form-item>

            <el-form-item
              label="手机号："
              style="width: 317px;margin:10px auto;margin-bottom:20px;font-size:12px"
            >
              <el-input v-model="infoFan.userPhone"></el-input>
            </el-form-item>
            <el-form-item
              label="身份证号："
              style="width: 317px;margin:10px auto;margin-bottom:20px;font-size:12px"
            >
              <el-input v-model="infoFan.idcardNo"></el-input>
            </el-form-item>

            <el-form-item
              label="登录账号："
              style="width: 317px;margin:10px auto;margin-bottom:20px;font-size:12px"
            >
              <el-input v-model="infoFan.userName" disabled></el-input>
            </el-form-item>

            <!-- <el-form-item
               label="登录密码："
               style="width:317px;margin:10px auto;font-size:12px !impotant;"
             >
               <el-input v-model="infoFan.userPwd"></el-input>
             </el-form-item>-->
            <el-form-item
              label="新密码："
              style="width:317px;margin:10px auto;font-size:12px !impotant;"
            >
              <el-input v-model="newPwd" placeholder="请输入新密码"></el-input>
            </el-form-item>
          </el-form>
        </div>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogV = false">取消</el-button>
          <el-button @click="sureAdd">确定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
  export default {
    name: "addAccount",
    data() {
      return {
        checkList: [],
        dialogVisible: false,
        dialogV: false,
        modelTitle: "添加账号",
        Title: "管理账号",
        ruleForm: { orgName: "", nickname: "" ,team:'',userPhone:'',idcardNo:''},
        ruleForms:{

        },
        rules:{
          orgName: [{ required: true, message: '请输入登录账号', trigger: 'blur' },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],
          userName: [{ required: true, message: '请输入登录账号', trigger: 'blur' },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],
          nickname: [{ required: true, message: '请输入登录密码', trigger: 'blur' },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],
          team:[{ required: true, message: '请选择施工队', trigger: 'change' }],
          userPhone: [{ required: true, message: "请输入手机号", trigger: "blur" },{ min: 1, max: 11, message: '长度在 1 到 11 个字符', trigger: 'blur' }],
          idcardNo: [{ required: true, message: "请输入身份证", trigger: "blur" }],
        },
        title: "施工队账号管理",
        info: [],//信息列表
        teamSearch: {
          team: "",
        },
        teamList: [],
        infoFan:{},//信息回显
        newPwd:'',//新密码
      };
    },
    mounted() {
      this.dataInit();
    },
    methods: {
      //管理信息回显
      /*displaymsg(id){
        let that = this;
        let suCb = function(res){
          that.infoFan = res.data.data;
        }
        let erCb = function(res){
          console.log(res)
        }
        let postData = {
          userId:id
        }
        let palyParam = {
          url: "/api/web/proTeam/echoAccount",
          methods:'post',
          data: postData,
          success: suCb,
          error: erCb,
        }
        that.$https(palyParam)
      },*/
      //点击管理
      manage(val){
        this.dialogV = true;
        this.infoFan = this.deepClone(val)
        // this.displaymsg(val.userId)
      },
      //确定修改
      sureAdd(){
        if(this.infoFan.userName == "" || this.infoFan.userName == undefined){
          this.$alert('账号或密码不能为空', '提示', {
            confirmButtonText: '确定',
          });
        }else{
          if(this.infoFan.userName.length>20 || this.newPwd.length>20){
            this.$message({
              message:'账号和新密码最长为20个字符',
              type:'error',
              duration:500
            })
            return false;
          }
          let that = this;
          let suCb = function(res){
            that.$message({
              message: '编辑成功',
              type: 'success'
            });
            that.dialogV = false;
            that.dataInit();
          }
          let erCb = function(res){
            console.log(res)
          }
          let postData = {
            userName: that.infoFan.userName.trim(),
            userPwd: "",
            userStatus: that.infoFan.userStatus,
            userId: that.infoFan.userId,
            userPhone: that.infoFan.userPhone,//手机号
            idcardNo: that.infoFan.idcardNo,//身份证id
          }
          let palyParam = {
            url: "/api/web/proTeam/editAccount",
            methods:'post',
            data: postData,
            success: suCb,
            error: erCb,
          }
          if(that.newPwd == "" || that.newPwd==undefined){
            let pwd = that.deepClone(that.ruleForms);
            palyParam.data.userPwd = that.infoFan.userPwd;
          }else{
            palyParam.data.userPwd = that.$md5(that.newPwd.trim());
          }
          that.$https(palyParam)
        }
      },
      //获取信息列表
      dataInit(){
        let that = this;
        let suCb = function(res){
          that.info = res.data.data;
          console.log(res.data.data)
        }
        let erCb = function(res){
          console.log(res)
        }
        let postData = {
        }
        let palyParam = {
          url: "/api/web/proTeam/getAccount",
          methods:'post',
          data: postData,
          success: suCb,
          error: erCb,
        }
        that.$https(palyParam)
      },
      //第一个点击添加账户
      addCount(){
        this.dialogVisible = true;
        this. ruleForm.orgName = "";
        this.ruleForm.nickname = "";
        this.ruleForm.team = "";
        this.ruleForm.userPhone = "";
        this.ruleForm.idcardNo = "";
        this.unAdd();
      },
      //未添加施工队账户
      unAdd(){
        let that = this;
        let suCb = function(res){
          that.teamList = res.data.data
          console.log(res.data.data)
        }
        let erCb = function(res){
          console.log(res)
        }
        let postData = {
        }
        let palyParam = {
          url: "/api/web/proTeam/getNoAccountTeam",
          methods:'post',
          data: postData,
          success: suCb,
          error: erCb,
        }
        that.$https(palyParam)
      },
      //添加施工队账号函数
      addCountFun(n1,n2){
        let that = this;
        let suCb = function(res){
          that.$message({
            message: '添加成功',
            type: 'success'
          });
          that.dataInit()
        }
        let erCb = function(res){
          console.log(res)
        }
        let postData = {
          userName: that.ruleForm.orgName.trim(),
          userPwd: "",
          userTruename: n1,
          userOrg: n2,
          userPhone: that.ruleForm.userPhone,//手机号
          idcardNo: that.ruleForm.idcardNo,//身份证id
        }
        let palyParam = {
          url: "/api/web/proTeam/addAccount",
          methods:'post',
          data: postData,
          success: suCb,
          error: erCb,
        }
        let pwd = that.deepClone(that.ruleForm);
        palyParam.data.userPwd = that.$md5(pwd.nickname.trim())
        that.$https(palyParam)
      },
      //确定添加施工队账号
      submitForm(formName){
        let that = this;
        that.$refs[formName].validate((valid) => {
          if (valid) {
            that.dialogVisible = false;
            let newArray = {};
            newArray = that.teamList.find((item) => {
              if(item.orgId === that.ruleForm.team){
                return item
              }
            });
            that.addCountFun(newArray.orgName,newArray.orgId)
          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      //点击取消
      resetForm(formName) {
        this.dialogVisible = false;
        this.$refs[formName].resetFields();
      }
    }
  };
</script>

<style scoped>
  .component-listHeader .search-header {
    width: 1100px;
    box-sizing: border-box;
    border: 1px solid rgba(228, 228, 228, 1);
    background-color: rgba(255, 255, 255, 1);
    margin: 20px auto 0;
    border-bottom: 0;
  }

  .component-listHeader .search-header .left-title {
    display: flex;
    justify-content: space-between;
    width: 100%;
    background-color: rgba(243, 243, 243, 1);
    height: 45px;
    box-sizing: border-box;
  }

  .component-listHeader .search-header .left-title .left-text {
    width: 20%;
    color: #666;
    line-height: 45px;
    font-family: "微软雅黑";
    font-weight: 400;
    font-size: 12px;
    text-align: left;
    margin: 0 0 0 30px;
  }

  .component-listHeader .search-header .right {
    width: 70%;
    display: flex;
    justify-content: flex-end;
    margin-right: 10px;
  }

  .component-listHeader .search-header .right button {
    padding: 0 15px;
    height: 30px;
    width: 80px;
    margin: 6px 5px;
    line-height: 30px;
    font-size: 14px;
    color: #666;
    background-color: #fff;
    border: 1px solid #ccc;
  }

  .component-listHeader .search-header .right button:hover {
    background-color: #f3f3f3;
  }

  .component-listHeader .search-header .right .right-select-margin {
    margin-top: 6px;
    margin-right: 8px;
  }

  .component-table-header {
    width: 1100px;
    margin: 0 auto;
    display: flex;
    border: 1px solid #e4e4e4;
    background-color: #f9fafc;
    height: 45px;
    line-height: 45px;
    padding: 0;
    list-style: none;
    justify-content: space-between;
    box-sizing: border-box;
    border-top: none;
  }

  .component-table-header li:first-child {
    width: 50px;
  }

  .component-table-header li:first-child span {
    border-radius: 5px;
    border: 2px solid #aba2a2;
    display: inline-block;
    width: 16px;
    height: 16px;
    margin: 12.5px auto;
    line-height: 16px;
    font-size: 16px;
  }

  .component-table-header li {
    border-right: 1px solid #e4e4e4;
    font-family: "PingFangSC-Semibold", "PingFang SC Semibold", "PingFang SC";
    font-weight: 650;
    font-style: normal;
    font-size: 14px;
    color: #666666;
  }
  .component-table-header li:last-child {
    border: none;
  }

  .component-table-body {
    width: 1100px;
    margin: 0 auto;
    display: flex;
    border: 1px solid #e4e4e4;
    background-color: #fff;
    height: 45px;
    line-height: 45px;
    padding: 0;
    list-style: none;
    justify-content: space-between;
    box-sizing: border-box;
    border-top: none;
  }

  .component-table-body li {
    font-size: 12px;
    color: #666;
    text-align: center;
    border-right: 1px solid #e4e4e4;
  }

  .component-table-body li:first-child {
    width: 50px;
  }

  .component-table-body li:nth-child(2) {
    width: 100px;
  }

  .component-table-body li:nth-child(3) {
    width: 150px;
  }

  .component-table-body li:nth-child(4) {
    width: 150px;
  }
  .component-table-body li:nth-child(5) {
    width: 150px;
  }

  .component-table-body li:last-child {
    width: 500px;
    border-right: none;
  }

  .component-table-body li:last-child {
    display: flex;
    justify-content: space-around;
    flex-wrap: wrap;
    padding: 0 auto;
  }

  .component-table-body li:last-child a {
    display: inline-block;
    text-align: center;
    color: #1abc9c;
    text-decoration: none;
    font-size: 12px;
  }

  .table {
    width: 1100px;
    margin: 0 auto;
  }
  /* 弹窗样式 */
  .model-input {
    width: 190px;
    height: 10px;
    border: 1px solid #ccc;
    padding: 10px;
    color: #ccc;
  }

  .input-box {
    margin-bottom: 100px;
  }
  .search {
    width: 40px;
    height: 30px;
    border: 1px solid #ccc;
    border-left: none;
    line-height: 30px;
    background-color: #ccc;
  }
  .search:hover {
    cursor: pointer;
  }

  .radio-group {
    padding-top: 30px;
    padding-left: 30px;
  }

  .radio-group .el-radio {
    height: 30px;
  }

  .addpro {
    display: inline-block;
    width: 100px;
    height: 30px;
    line-height: 30px;
    border: 1px solid #ccc;
    background-color: #fff;
    color: #ccc;
  }
  .addpro:hover {
    cursor: pointer;
  }

  .upload:hover {
    cursor: pointer;
  }

  .btn-search {
    display: inline-block;
    float: right;
    width: 80px;
    height: 40px;
    background-color: #1abc9a;
    margin-top: 50px;
    color: #fff;
    border-radius: 5px;
    font-size: 15px;
    line-height: 40px;
  }
  .btn-search:hover {
    cursor: pointer;
  }

  .el-table .cell a {
    color: #1abc9a;
    text-decoration: none;
    cursor: pointer;
  }
</style>
